
<template>
  <el-form :model="form" ref="form" :rules="rules" class="form">
    <el-form-item class="form-item" prop="username">
      <el-input placeholder="用户名手机" v-model="form.username" @focus="clearRules('username')"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="captcha">
      <el-input placeholder="验证码" v-model="form.captcha" @focus="clearRules('captcha')">
        <template slot="append">
          <el-button @click="handleSendCaptcha">发送验证码</el-button>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="nickname">
      <el-input placeholder="你的名字" v-model="form.nickname" @focus="clearRules('nickname')"></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="password">
      <el-input
        placeholder="密码"
        type="password"
        v-model="form.password"
        @focus="clearRules('password')"
      ></el-input>
    </el-form-item>

    <el-form-item class="form-item" prop="confirmpass">
      <el-input
        placeholder="确认密码"
        type="password"
        v-model="form.confirmpass"
        @focus="clearRules('confirmpass')"
      ></el-input>
    </el-form-item>

    <el-button class="submit" type="primary" @click="handleRegSubmit">注册</el-button>
  </el-form>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
        // this往外指
      } else if (value !== this.form.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      // 表单数据
      form: {
        username: "", // 登录用户名/手机
        nickname: "", // 昵称
        captcha: "", // 手机验证码
        password: "", // 登录密码
        confirmpass: "" // 确认密码
      },

      // 表单规则
      rules: {
        //表单里的属性
        username: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 7, max: 11, message: "长度在 7 到 11 个字符", trigger: "blur" }
        ],
        nickname: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 5, max: 15, message: "长度在 5 到 15 个字符", trigger: "blur" }
        ],
        captcha: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          { min: 6, max: 6, message: "长度6个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 9, message: "长度在 6 到 9 个字符", trigger: "blur" }
        ],
        confirmpass: [{ validator: validatePass, trigger: "blur" }]
      }
    };
  },
  methods: {
    // 发送验证码
    handleSendCaptcha() {
      this.$axios({
        url: "/captchas",
        method: "post",
        data: {
          tel: this.form.username
        }
      }).then(res => {
        console.log(res.data);
        this.$confirm("暂时验证码为:000000", "提示", {
          type: "warning",
          showCancelButton: false
        });
      });
    },

    // 注册
    handleRegSubmit() {
      //发送前的一次保底总校验,希望无需触发器, 也想将整个表单全部验证一次
      this.$refs.form.validate((isrules, mistake) => {
        if (isrules) {
          // 注意,请求的数据没有confirmpass,所以可以用剩余解构
          const { confirmpass, ...Residue } = this.form;
          this.$axios({
            url: "/accounts/register",
            method: "post",
            data: Residue
          }).then(res => {
            console.log(res);
            this.$message({
              message: "注册成功,正在跳转...",
              type: "success"
            });
            //跳转到登陆页面
            setTimeout(() => {
              this.$emit("tologin");
            }, 1000);
          });
        } else {
          console.log(mistake);
        }
      });
      console.log(this.form);
    },

    // 输入框聚焦时, 暂时清楚错误提示,clearValidate移除表单项的校验结果
    clearRules(propinfo) {
      this.$refs.form.clearValidate(propinfo);
    }
  }
};
</script>

<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>